<template>
  <div class="back">
    <div class="login">
      <el-form :model="userInfor"
               status-icon
               :rules="rules"
               ref="userInfor"
               class="demo-userInfor">
        <h3 class="title">后台管理系统</h3>
        <el-form-item prop="username">
          <el-input type="text"
                    prefix-icon="el-icon-user"
                    placeholder="账号"
                    v-model="userInfor.username"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    prefix-icon="el-icon-lock"
                    placeholder="密码"
                    v-model="userInfor.password"
                    autocomplete="off"></el-input>
        </el-form-item>
        <div class="toregister">
          <router-link to="register">前往注册>>></router-link>
        </div>
        <el-button class="elButton"
                   type="primary"
                   :loading="loading"
                   :disabled="loading"
                   @click="submitForm('userInfor')">{{buttonText}}</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    var validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      buttonText: '登录',
      loading: false,
      userInfor: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { validator: validateName, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      var _this = this
      this.loading = true
      this.buttonText = '登录中...'
      this.$refs[formName].validate((valid) => {
        // console.log(getUser)
        this.$axios({
          url: '/user/login',
          method: 'post',
          dataType: 'json',
          data: {
            username: this.userInfor.username,
            password: this.userInfor.password
          }
        }).then(successResponse => {
          if (successResponse.data.code === 200) {
            this.$message.success('登录成功')
            localStorage.setItem('admin', this.userInfor.username)
            setTimeout(function () {
              _this.$router.push({ name: 'home' })
            }, 1000)
          } else {
            this.$message.error('账号密码错误')
            this.loading = false
            this.buttonText = '登录'
          }
          console.log(successResponse)
        }).catch(failResponse => {
          console.log(failResponse)
          var errorStr = failResponse.toString()
          this.$message.error(errorStr)
          this.loading = false
          this.buttonText = '登录'
        })
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';

.back {
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  height: 100%;
  background-image: $backImage;
  background-size: cover;
}

.login {
  background: #fff;
  padding: 0.5rem;
  width: 7rem;
  border-radius: 0.4rem;

  .title {
    font-weight: bold;
    font-size: 0.4rem;
    margin: 0 auto 30px;
    text-align: center;
    color: #707070;
  }

  .toregister {
    float: right;
    padding-bottom: 0.3rem;
  }

  .elButton {
    width: 100%;
  }
}
</style>
